package com.example.demo.service.Impl;

import com.example.demo.common.domain.Leave_Record;
import com.example.demo.common.domain.OutRecord;
import com.example.demo.common.domain.OutRecordExample;
import com.example.demo.common.domain.OutRecordPlus;
import com.example.demo.mapper.OutRecordMapper;
import com.example.demo.service.OutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("outServiceImpl")
public class OutServiceImpl implements OutService {
    @Autowired
    private OutRecordMapper outRecordMapper;

    @Override
    public List<OutRecordPlus> selectAllPlus() {
        return outRecordMapper.selectAllPlus();
    }

    @Override
    public void updateGIdByPrimaryId(Integer oid, Integer gid) {
        OutRecord outRecord = outRecordMapper.selectByPrimaryKey(oid);
        outRecord.setGid(gid);
        outRecord.setOgeneralagree((short)1);
        outRecordMapper.updateByPrimaryKey(outRecord);
    }

    @Override
    public void updateDIdByPrimaryId(Integer oid, Integer did) {
        OutRecord outRecord = outRecordMapper.selectByPrimaryKey(oid);
        outRecord.setDid(did);
        outRecord.setOdeputyagree((short)1);
        outRecordMapper.updateByPrimaryKey(outRecord);
    }

    @Override
    public void updateDivIdByPrimaryId(Integer oid, Integer divid) {
        OutRecord outRecord = outRecordMapper.selectByPrimaryKey(oid);
        outRecord.setDivid(divid);
        outRecord.setOdividagree((short)1);
        outRecordMapper.updateByPrimaryKey(outRecord);
    }

    @Override
    public List<OutRecordPlus> GeneralSelectAllOut() {
        return outRecordMapper.GeneralSelectAllOut();
    }

    @Override
    public List<OutRecordPlus> DeputySelectAllOut() {
        return outRecordMapper.DeputySelectAllOut();
    }

    @Override
    public List<OutRecord> selectAll() {
        OutRecordExample example = new OutRecordExample();
        example.createCriteria();
        return outRecordMapper.selectByExample(example);
    }

    @Override
    public OutRecord selectByPrimaryId(Integer oid) {
        OutRecordExample example = new OutRecordExample();
        example.createCriteria().andOidEqualTo(oid);
        List<OutRecord> outRecords = outRecordMapper.selectByExample(example);
        return outRecords.size() ==0?null:outRecords.get(0);
    }

    @Override
    public void insertSelective(OutRecord outRecord) {
        List<OutRecord> outRecords = outRecordMapper.selectByExample(null);
        if(outRecords.size() != 0){
            int lastOid = outRecords.get(outRecords.size()-1).getOid();
            outRecord.setOid(lastOid+1);
        }else{
            outRecord.setOid(1);
        }

        outRecordMapper.insertSelective(outRecord);
    }
}
